home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ETO Development Tools 4
/
ETO Development Tools 4.iso
/
Tools - Objects
/
MacApp
/
MacApp 3.0a2
/
CPlusIncludes
/
Geometry.h
< prev
next >
Wrap
Text File
|
1991-05-01
|
8KB
|
297 lines
// Geometry.h
// Copyright 1984-1991 Apple Computer, Inc. All rights reserved.
#ifndef __GEOMETRY__
#define __GEOMETRY__
#ifndef __TYPES__
#include <Types.h>
#endif
//--------------------------------------------------------------------------------
// Selector operator indicator enumeration. Which coordinate do we want
// to select?
//--------------------------------------------------------------------------------
enum VHSelect { vSel, hSel };
//--------------------------------------------------------------------------------
// SimplePoint: is used as the field type for some structs in the toolbox that
// we don't want promoted to a class; either because of constructor problems or
// the struct may appear in a union. Simple point is also used as the base class
// for Point; we simply inherit the data members.
//--------------------------------------------------------------------------------
struct SimplePoint
{
short v;
short h;
};
//--------------------------------------------------------------------------------
// Point: is a toolbox compatible class for the old struct Point. It is bitwise
// compatible with the old struct because it contains no virtual functions.
//--------------------------------------------------------------------------------
typedef struct Point *PointPtr;
struct Point : public SimplePoint
{
// Constructors
Point(); // Default constructor.
Point(short iV,
short iH);
Point(const SimplePoint& pt);
// Copy simply returns a copy of the object. Useful for creating automatic copies
// of an object on the stack, in a function call.
Point Copy() const;
// Conversion operator, for converting Point to a textual representation of a
// Point. "Point(v,h)".
operator char*() const;
// Selector operators, two are needed one for operating on const Points
// and the other on non-const Points.
short& operator[](VHSelect sel); // For non-const Point
const short& operator[](VHSelect sel) const;// For const Point
// Arithmetic operators
Point operator+(const Point& pt) const; // c = a + b (a,b and c are Points)
Point operator-(const Point& pt) const; // c = a - b (a,b and c are Points)
Point& operator+=(const Point& pt); // c += a (a and c are points, and
// a reference to a is returned,
// so these can be strung together)
Point& operator-=(const Point& pt); // c -= a ( ditto )
// Relational operators
Boolean operator!=(const Point& pt) const; // a != b
Boolean operator==(const Point& pt) const; // a == b
Boolean operator>(const Point& pt) const; // a > b
Boolean operator<(const Point& pt) const; // a < b
Boolean operator>=(const Point& pt) const; // a >= b
Boolean operator<=(const Point& pt) const; // a <= b
};
//--------------------------------------------------------------------------------
// Inline method definitions for Point.
//--------------------------------------------------------------------------------
inline Point::Point(short iV, short iH)
{
v = iV;
h = iH;
}
inline Point::Point(const SimplePoint& pt)
{
v = pt.v;
h = pt.h;
}
inline Point Point::Copy() const
{
Point pt;
pt.v = v;
pt.h = h;
return pt;
}
//--------------------------------------------------------------------------------
// Selector operator indicator enumeration. Which Point do we want
// to select, top-left or bottom-right?
//--------------------------------------------------------------------------------
enum PointSelector { topLeft, botRight };
//--------------------------------------------------------------------------------
// SimpleRect: is used as the field type for some structs in the toolbox that
// we don't want promoted to a class; either because of constructor problems or
// the struct may appear in a union. Simple Rect is also used as the base class
// for Rect; we simply inherit the data members.
//--------------------------------------------------------------------------------
struct SimpleRect
{
short top;
short left;
short bottom;
short right;
};
//--------------------------------------------------------------------------------
// Rect: is a toolbox compatible class for the old struct Rect. It is bitwise
// compatible with the old struct because it contains no virtual functions.
//--------------------------------------------------------------------------------
typedef struct Rect *RectPtr;
struct Rect : public SimpleRect
{
// Rect constructors
Rect();
Rect(short iTop,
short iLeft,
short iBottom,
short iRight);
Rect(const Rect& rect);
Rect(const Point& topLeftPt,
const Point& botRightPt);
Rect(const SimpleRect& rect);
// Copy simply returns a copy of the object. Useful for creating automatic copies
// of an object on the stack, in a function call.
Rect Copy() const;
// Conversion operator, for converting Rect to a textual representation of a
// Rect. "Rect(top,left,bottom,right)".
operator char*() const;
// Point selectors for Rect. One for operating on const Rects and one
// for non-const Rects.
Point& operator[](PointSelector sel); // Selector for non-const Rects
const Point& operator[](PointSelector sel) const; // Selector for const Rects
// Operators for adding and subtracting one Rect from to/from another.
Rect operator+(const Rect& rt) const;
Rect operator-(const Rect& rt) const;
Rect& operator+=(const Rect& rt);
Rect& operator-=(const Rect& rt);
// Operators overloaded for adding and subtracting some increment to/from each point
// along both axis. Very convenient for translating Rects. These take a point and since
// Point has a constructor that takes two shorts the Rect windowRect can be translated
// 100 pixels in the positive y direction by the statement:
//
// windowRect = windowRect + Point (0, 100); or
// windowRect += Point (0, 100);
Rect operator+(const Point& pt) const;
Rect operator-(const Point& pt) const;
Rect& operator+=(const Point& pt);
Rect& operator-=(const Point& pt);
// Inset a Rect by Point.
Rect& Inset(const Point& delta);
// Equality operators, other relational operator could be defined such as <. But their
// meaning is less clear and probably better implemented as methods. For example,
// aRect < bRect could return true if aRect was inside of bRect, or could return true
// if the area of aRect was less than the area of bRect.
Boolean operator==(const Rect& rt) const;
Boolean operator!=(const Rect& rt) const;
// Two simple area operators, the intersection && (logical and in C++) and the
// union || (logical or in C++). The definition of union here is to return a Rect
// that exactly encloses its operands.
Rect operator&&(const Rect&) const;
Rect operator||(const Rect& rt) const;
// Returns true if a valid rectangle (left < right and top < bottom). If not
// a valid rectangle then return false and set all coordinates to 0.
Boolean ValidRect();
// Return true if the Rect is empty (right - left = 0 and bottom - top = 0)
Boolean Empty() const;
// Returns the length of a Rect in a given dimension.
short Length(VHSelect sel) const;
// Returns the size of a Rect in both dimensions. (a Point)
Point Size() const;
// The Contains method takes either a Point or a Rect and returns true if the operand
// is inside of the Rect the method is applied to.
Boolean Contains(const Point& pt) const;
Boolean Contains(const Rect& rt) const;
private:
short Min(const short a,
const short b) const;
short Max(const short a,
const short b) const;
};
//--------------------------------------------------------------------------------
// Inline definitions for methods in Rect
//--------------------------------------------------------------------------------
inline Rect::Rect() { }
inline Rect::Rect(short iTop, short iLeft, short iBottom, short iRight)
{
top = iTop;
left = iLeft;
bottom = iBottom;
right = iRight;
}
inline Rect::Rect(const Rect& rect)
{
top = rect.top;
left = rect.left;
bottom = rect.bottom;
right = rect.right;
}
inline Rect::Rect(const Point& topLeftPt, const Point& botRightPt)
{
top = topLeftPt.v;
left = topLeftPt.h;
bottom = botRightPt.v;
right = botRightPt.h;
}
inline Rect::Rect(const SimpleRect& rect)
{
top = rect.top;
left = rect.left;
bottom = rect.bottom;
right = rect.right;
}
inline Rect Rect::Copy() const
{
Rect rt;
rt.top = top;
rt.left = left;
rt.bottom = bottom;
rt.right = right;
return rt;
}
#endif